//
//  YWUserInfo.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/28.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWUserInfo.h"


@implementation YWUserInfo

#pragma mark - 单例模式
static YWUserInfo *_userInfo;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    // 使用GCD的一次性代码
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _userInfo = [super allocWithZone:zone];
    });
    return _userInfo;
}

+ (instancetype)sharedUseInfo
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _userInfo = [[self alloc] init];
    });
    return _userInfo;
}

#pragma mark - 用户名
- (NSString *)name{
    return [self objectForKey:@"name"];
}

- (void)setName:(NSString *)name{
    [self setObject:name forKey:@"name"];
}

#pragma mark - 密码
- (NSString *)password{
    return [self objectForKey:@"password"];
}

- (void)setPassword:(NSString *)password{
    [self setObject:password forKey:@"password"];
}

#pragma mark - workId
- (NSString *)workId{
    return [self objectForKey:@"workId"];
}

- (void)setWorkId:(NSString *)workId{
    [self setObject:workId forKey:@"workId"];
}

#pragma mark - accessToken
- (NSString *)accessToken{
    return [self objectForKey:@"accessToken"];
}

- (void)setAccessToken:(NSString *)accessToken{
    [self setObject:accessToken forKey:@"accessToken"];
}

#pragma mark - photoQuality
- (NSNumber *)photoQuality{
    
    // 默认是1 中等品质
    if ([self objectForKey:@"photoQuality"] == nil) {
        return @1;
    }
    
    return [self objectForKey:@"photoQuality"];
}

- (void)setPhotoQuality:(NSNumber *)photoQuality{
    [self setObject:photoQuality forKey:@"photoQuality"];
}

#pragma mark - 上次登录时间
- (NSDate *)lastLoginTime{
    
    return [self objectForKey:@"lastLoginTime"];

}

- (void)setLastLoginTime:(NSDate *)lastLoginTime{
    
    [self setObject:lastLoginTime forKey:@"lastLoginTime"];
}

#pragma mark - sampleTypeList
- (NSMutableArray *)sampleTypeList{
    if (_sampleTypeList == nil) {
        // 获取Cache文件
        NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [cachePaht stringByAppendingPathComponent:@"sampleTypeList.plist"];
        _sampleTypeList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (_sampleTypeList == nil) {
            _sampleTypeList = [NSMutableArray array];
        }
    }
    return _sampleTypeList;
}

- (NSMutableArray *)groupList{
    if (_groupList == nil) {
        // 获取Cache文件
        NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [cachePaht stringByAppendingPathComponent:@"groupList.plist"];
        _groupList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (_groupList == nil) {
            _groupList = [NSMutableArray array];
        }
    }
    return _groupList;
}

- (NSMutableArray *)areaList{
    if (_areaList == nil) {
        // 获取Cache文件
        NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [cachePaht stringByAppendingPathComponent:@"areaList.plist"];
        _areaList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (_areaList == nil) {
            _areaList = [NSMutableArray array];
        }
    }
    return _areaList;

}

#pragma mark - 偏好设置
- (id)objectForKey:(NSString *)defaultName{
    return  [[NSUserDefaults standardUserDefaults] objectForKey:defaultName];
}


- (void)setObject:(id)value forKey:(NSString *)defaultName
{
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
}

@end
